package springinpractice.ch4.r4_5.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.hibernate.annotations.CollectionOfElements;
import org.springmodules.validation.bean.conf.loader.annotation.handler.Email;
import org.springmodules.validation.bean.conf.loader.annotation.handler.Expression;
import org.springmodules.validation.bean.conf.loader.annotation.handler.MaxLength;
import org.springmodules.validation.bean.conf.loader.annotation.handler.NotBlank;

/*
 * Author       :       Willie Wheeler and John Wheeler
 * Book         :       'Spring In Practice' [Book written by Willie Wheeler and John Wheeler]
 */

@Entity
@Table(name = "users")
public class User {
	private static final String DEFAULT_ROLE = "ROLE_USER";
	
	private Long id;
	
	@NotBlank
	@MaxLength(40)
	private String firstName;
	
	@NotBlank
	@MaxLength(40)
	private String lastName;
	
	@NotBlank
	@MaxLength(20)
	private String username;
	
	@NotBlank
	@MaxLength(80)
	@Email
	private String email;
	
	@NotBlank
	@MaxLength(20)
	@Expression(value = "password equals passwordConfirmation", 
			errorCode = "password.not.match")
	private String password;
	
	private String passwordConfirmation;
	private boolean enabled = true;
	private Set<String> roles = new HashSet<String>();
	
	public User() {
		addRole(DEFAULT_ROLE);
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	public Long getId() {
		return id;
	}

	// This must be public for CXF Aegis databinding (web service)
	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(name = "first_name")
	public String getFirstName() {
		return firstName;
	}
	
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	@Column(name = "last_name")
	public String getLastName() {
		return lastName;
	}
	
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
		
	@Column(name = "username")
	public String getUsername() {
		return username;
	}	
	
	public void setUsername(String userName) {
		this.username = userName;
	}
		
	@Column(name = "email")
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	@Column(name = "password")
	public String getPassword() {
		return password;
	}	
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Transient
	public String getPasswordConfirmation() {
		return passwordConfirmation;
	}

	public void setPasswordConfirmation(String passwordConfirmation) {
		this.passwordConfirmation = passwordConfirmation;
	}
	
	@Column(name = "enabled")
	public boolean isEnabled() {
		return enabled;
	}
	
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	@CollectionOfElements(targetElement = java.lang.String.class)
	@JoinTable(name = "roles", joinColumns = @JoinColumn(name = "user_id"))
	@Column(name = "role")	
	public Set<String> getRoles() {
		return roles;
	}
		
	public void setRoles(Set<String> roles) {
		this.roles = roles;
	}
	
	public void addRole(String role) {
		roles.add(role);
	}

	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
			.append("id", id)
			.append("firstName", firstName)
			.append("lastName", lastName)
			.append("userName", username)
			.append("email", email)
			.append("enabled", enabled)
			.toString();			
	}		
}
